package cn.edu.nju.software.cdiy.action.supplier;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.bean.Supplier;
import cn.edu.nju.software.cdiy.service.Service;
import cn.edu.nju.software.cdiy.util.BusinessConstant;
import cn.edu.nju.software.cdiy.util.Constant;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("serial")
public class AddHardwareAction<T extends Hardware> extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware, Preparable {

    private Map<String, Object> session;
	private HttpServletResponse response;
	private HttpServletRequest request;
	
    private Service<T> service;
	private Map<String,List<String>> parameterMap;
    private Map<String,List<String>> resultMap;

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		this.response.setCharacterEncoding("UTF-8");
	}

	public void setService(Service<T> service) {
		this.service = service;
	}

	public Map<String, List<String>> getParameterMap() {
		return parameterMap;
	}

	public void setParameterMap(Map<String, List<String>> parameterMap) {
		this.parameterMap = parameterMap;
	}

	public Map<String, List<String>> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, List<String>> resultMap) {
		this.resultMap = resultMap;
	}

	@Override
	public void prepare() throws Exception {
		Map<String, String[]> parametersMap = request.getParameterMap();
		resultMap = new HashMap<String, List<String>>();
		for (String parameterName : parametersMap.keySet()) {
			String parameterValue = parametersMap.get(parameterName)[0];
			//System.out.println(parameterName + ": " + parameterValue);
			/*如果符合map[name][0]的格式就对resultMap进行填充*/
			if (parameterName.matches("^\\w+\\[[\\w\u4e00-\u9fa5]+\\]\\[\\d+\\]$")) {
				String key = parameterName.substring(parameterName.indexOf('[')+1, parameterName.indexOf(']'));
				if (resultMap.containsKey(key)) {
					List<String> valueList = resultMap.get(key);
					if (!valueList.contains(parameterValue)) {
						valueList.add(parameterValue);
					}
				} else {
					List<String> valueList = new LinkedList<String>();
					valueList.add(parameterValue);
					resultMap.put(key, valueList);
				}
			}
		}
	}

	@Override
	public String input() throws Exception {
		parameterMap = service.getInsertboard();
		return INPUT;
	}

	public void  add() throws Exception {
		/*System.out.println("---===AddHardwareAction.add()===---");
		if (resultMap == null) {
			System.out.println("resultMap is null!");
		} else {
			System.out.println("resultMap.size(): " + resultMap.size());
			for (String key : resultMap.keySet()) {
				System.out.println(key + ": " + resultMap.get(key));
			}
		}*/
		Supplier supplier = (Supplier) session.get(BusinessConstant.S_SUPPLIER);
		List<String> list = new ArrayList<String>();
		list.add("" + supplier.getId());
		resultMap.put(Constant.commonInforStr[9], list);
		if (service.insertHardware(resultMap)) {
			response.getWriter().write("添加成功！");
		} else {
			response.getWriter().write("添加失败！");
		}
	}
	
}
